package com.lbn.monitor.widget;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.free.framework.base.BaseActivity;
import com.free.framework.lib.utils.ScreenUtils;
import com.free.framework.ui.IAppbar;
import com.lbn.monitor.R;

/**
 * @author 李炳南
 * @date 2017/3/6
 */
public class Appbar implements IAppbar {

    private View appbar;

    private Activity context;
    public Appbar(Activity context) {
        this.context = context;
        appbar = LayoutInflater.from(context).inflate(R.layout.layout_appbar, null, false);
    }

    @Override
    public void attachToActivity(BaseActivity baseActivity) {

        ViewGroup decorView = (ViewGroup) baseActivity.getWindow().getDecorView();
        View contentView = ((ViewGroup) decorView.findViewById(android.R.id.content)).getChildAt(0);


        LinearLayout parent = new LinearLayout(baseActivity);
        parent.setOrientation(LinearLayout.VERTICAL);
        parent.addView(appbar, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        ViewGroup viewParent = (ViewGroup) contentView.getParent();
        viewParent.removeView(contentView);
        parent.addView(contentView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        viewParent.addView(parent);

        View barRootView = appbar.findViewById(R.id.bar_root_view);
        ScreenUtils.setStatusBarHeight(baseActivity,barRootView);
    }



    public Appbar setTitle(CharSequence title) {
        TextView tle = findViewById(R.id.title);
        tle.setText(title);
        tle.setVisibility(View.VISIBLE);
        return this;
    }


    public Appbar openBack() {
        TextView back = findViewById(R.id.back);
        back.setOnClickListener(backListener());
        back.setVisibility(View.VISIBLE);
        return this;
    }


    public Appbar openBack(int image) {
        ImageButton back = findViewById(R.id.back);
        back.setOnClickListener(backListener());
        back.setImageResource(image);
        back.setVisibility(View.VISIBLE);
        return this;
    }


    public Appbar leftImageButton(int image, View.OnClickListener listener) {
        return this;
    }


    public Appbar rightImageButton(int image, View.OnClickListener listener) {
        ImageButton imageButton = findViewById(R.id.right_image_button);
        imageButton.setImageResource(image);
        imageButton.setOnClickListener(listener);
        imageButton.setVisibility(View.VISIBLE);
        return this;
    }


    public Appbar leftButton(CharSequence button, View.OnClickListener listener) {
        return this;
    }


    public Appbar rightButton(CharSequence cs, View.OnClickListener listener) {
        TextView button = findViewById(R.id.right_button);
        button.setText(cs);
        button.setOnClickListener(listener);
        button.setVisibility(View.VISIBLE);
        return this;
    }


    public <T extends View> T findViewById(int id) {
        return (T) appbar.findViewById(id);
    }


    private View.OnClickListener backListener(){
        return new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.finish();
            }
        };
    }


}
